package com.polaris.live.im.db.dao

import androidx.room.Dao
import androidx.room.Insert
import androidx.room.OnConflictStrategy
import androidx.room.Query
import com.polaris.live.im.db.entity.SessionEntity
import com.polaris.live.im.db.entity.bo.SessionUnreadCountBean

/**
 * 消息列表
 *
 * @author Created by 半仙 on 2023/8/10/010 15:48
 */
@Dao
interface SessionListEntityDao {

    /**
     * 清除表所有数据
     */
    @Query("delete from session")
    suspend fun truncateTable()

    //插入消息
    @Insert(onConflict = OnConflictStrategy.REPLACE)
    suspend fun saveSession(sessionEntity: SessionEntity): Long

    //查找是否有
    @Query("select * from session where user_id = :userId and other_id = :otherId")
    suspend fun getSession(userId: Long, otherId: Long): SessionEntity?

    //分页查找会话列表
    @Query("select * from session where user_id = :userId and not stranger_status and other_id != 0 order by timestamp desc limit :limit offset :offset")
    suspend fun getSessions(userId: Long, limit: Int, offset: Int): List<SessionEntity>

    //分页按陌生人查找会话列表
    @Query("select * from session where user_id = :userId and (stranger_status or stranger_status is null) and other_id != 0 order by timestamp desc limit :limit offset :offset")
    suspend fun getSessionsByStranger(userId: Long, limit: Int, offset: Int): List<SessionEntity>

    //获取所有陌生人会话ids
    @Query("select other_id from session where user_id = :userId and (stranger_status or stranger_status is null) and other_id != 0")
    suspend fun getAllSessionIdsByStranger(userId: Long): List<Long>

    //删除某条信息
    @Query("delete from session where user_id = :userId and other_id = :otherId")
    suspend fun deleteSession(userId: Long, otherId: Long): Int

    //获取一天前的陌生人会话
    @Query(
        """
        select other_id
        from session 
        where user_id = :userId 
          and other_id != 0
          and sender_id != :userId 
          and timestamp <= strftime('%s', 'now', '-1 day') * 1000
          and ((stranger_status or stranger_status is null) or (not stranger_status and greet_push_message))
        """
    )
    suspend fun getAllSessionByStrangerAndOneDayAgo(userId: Long): List<Long>

    //修改用户信息
    @Query("update session set user_json = :userJson where user_id = :userId and other_id = :otherId")
    suspend fun updateSessionUserByOtherId(userId: Long, otherId: Long, userJson: String)

    //将超时的打招呼更新为陌生人消息
    @Query(
        """
        update session 
        set stranger_status = 1
        where user_id = :userId 
          and not stranger_status and (greet_push_message and timestamp <= strftime('%s', 'now', '-1 hour') * 1000)
          and other_id != 0 
    """
    )
    suspend fun updateGreetMessageTimeoutToStranger(userId: Long)

    //修改未读数
    @Query("update session set unread_count = :unreadCount where user_id = :userId and other_id = :otherId")
    suspend fun updateUnreadCountByOtherId(userId: Long, otherId: Long, unreadCount: Long)

    //修改未读数
    @Query("update session set unread_count = :unreadCount, stranger_unread_count = :strangerUnreadCount where user_id = :userId and other_id = :otherId")
    suspend fun updateAllUnreadCountByOtherId(userId: Long, otherId: Long, unreadCount: Long, strangerUnreadCount: Long)

    //获取用户的所有未读数
    @Query("select sum(unread_count) from session where user_id = :userId")
    suspend fun getTotalUnreadCount(userId: Long): Long?

    //获取陌生人会话的未读数
    @Query("select sum(unread_count) from session where user_id = :userId and other_id != 0 and (stranger_status or stranger_status is null)")
    suspend fun getUnreadCountByStranger(userId: Long): Long?

    //获取会话的未读数
    @Query("select sum(unread_count) unreadCount, sum(stranger_unread_count) strangerUnreadCount from session where user_id = :userId and other_id = :otherId")
    suspend fun getUnreadCountByOtherId(userId: Long, otherId: Long): SessionUnreadCountBean?

    //清除陌生人会话的主未读数
    @Query("update session set unread_count = 0 where user_id = :userId and other_id != 0 and (stranger_status or stranger_status is null)")
    fun clearMainUnreadCountByStranger(userId: Long)

    //将会话更新为陌生人消息
    @Query("update session set stranger_status = 1 where user_id = :userId and other_id = :otherId")
    fun updateSessionToStrangerUserByOtherId(userId: Long, otherId: Long)

}